/* Replication file for "Now or Later?"*/
/* Regression analysis in "Turnout before vs. on Election Day"*/

use before_vs_onelectionday.dta, clear

* Variables 
egen km = group(ken muni)
tab ken, gen(kk)
gen time1 = time == 1
gen time2 = time == 2
gen time1drain = time1 * drain
gen time2drain = time2 * drain
gen time1vmargin = time1 * vmargin
gen time2vmargin = time2 * vmargin
gen time1lpop = time1 * lpop
gen time2lpop = time2 * lpop
gen time1p65 = time1 * p65
gen time2p65 = time2 * p65
gen time1pcollege = time1 * pcollege
gen time2pcollege = time2 * pcollege
egen muniid = group(muni)


* Table 5
estpost summarize turnout_17_14 rain_17_14 drain_17_14
estout, style(tex) cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3))") label
eststo clear
estpost summarize turnout_17_14 rain_17_14 if time == 1 
estout, style(tex) cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3))") label
eststo clear
estpost summarize turnout_17_14 rain_17_14 if time == 2
estout, style(tex) cells("mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3))") label
eststo clear


* Table 6
qui eststo: reg turnout_17_14 ibn.time rain_17_14, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time rain_17_14 kk1-kk9 kk11-kk47, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14 rain_17_14, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14 rain_17_14 kk1-kk9 kk11-kk47, noconstant cluster(km)
estout, style(tex) cells(b(star fmt(3) ) se(par fmt(3))) label  collabels(none)  stats(r2_a N) starlevels($*$ 0.10 $**$ 0.05 $***$ 0.01) drop(kk*)
eststo clear

* Table 7
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14  ibn.time#c.vmargin rain_17_14  kk1-kk9 kk11-kk47, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14  ibn.time#c.lpop rain_17_14  kk1-kk9 kk11-kk47, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14  ibn.time#c.p65 rain_17_14  kk1-kk9 kk11-kk47, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14  ibn.time#c.pcollege rain_17_14  kk1-kk9 kk11-kk47, noconstant cluster(km)
estout, style(tex) cells(b(star fmt(3) ) se(par fmt(3))) label  collabels(none)  stats(r2_a N) starlevels($*$ 0.10 $**$ 0.05 $***$ 0.01) drop(kk*)
eststo clear

* Table 8
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14 rain_17_14 kk1-kk9 kk11-kk47 if m65 == 0, noconstant cluster(km)
qui eststo: reg turnout_17_14 ibn.time ibn.time#c.drain_17_14 rain_17_14 kk1-kk9 kk11-kk47 if m65 == 1, noconstant cluster(km)
estout, style(tex) cells(b(star fmt(3) ) se(par fmt(3))) label  collabels(none)  stats(r2_a N) starlevels($*$ 0.10 $**$ 0.05 $***$ 0.01) drop(kk*)
eststo clear

* Table A.4 
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain rain_17_14 kk1-kk9 kk11-kk47, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain  time1vmargin time2vmargin rain_17_14 kk1-kk9 kk11-kk47, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain  time1lpop time2lpop rain_17_14 kk1-kk9 kk11-kk47, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain  time1p65 time2p65 rain_17_14 kk1-kk9 kk11-kk47, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain  time1pcollege time2pcollege rain_17_14 kk1-kk9 kk11-kk47, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
estout, style(tex) cells(b(star fmt(3) ) se(par fmt(3))) label  collabels(r2_a N) starlevels($*$ 0.10 $**$ 0.05 $***$ 0.01) drop(kk*)
eststo clear
  
* Table A.5  
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain rain_17_14 kk1-kk9 kk11-kk47 if m65 == 0, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
qui eststo: ols_spatial_HAC turnout_17_14 time1 time2 time1drain time2drain rain_17_14 kk1-kk9 kk11-kk47 if m65 == 1, ///
	lat(POINT_Y) lon(POINT_X) t(time) p(muniid) dist(20) lag(1) bartlett disp
estout, style(tex) cells(b(star fmt(3) ) se(par fmt(3))) label  collabels(r2_a N) starlevels($*$ 0.10 $**$ 0.05 $***$ 0.01) drop(kk*)
eststo clear


* Figure 5
replace rain_17_14 = 0 if time == 1
collapse(sum) rain_17_14 turnout17 turnout14, by(ken muni)
gen turnout_17_14 = turnout17 - turnout14
twoway (scatter turnout_17_14  rain_17_14) (lowess turnout_17_14  rain_17_14), ///
  legend(off) xtitle("Change in Election Day precipitation") ytitle("Change in overall turnout") ///
  name(overallturnout, replace) xsize(5) ysize(4)
